package org.example.day11;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.12.1 isFair() 判断是不是公平锁
 **/
public class MyThread12 {
    private ReentrantLock lock;
    public MyThread12(boolean isFair){
        lock = new ReentrantLock(isFair);
    }
    public void serviceMethod(){
        try {
            lock.lock();
            System.out.println("判断锁是否公平" + lock.isFair());
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        final MyThread12 m12a = new MyThread12(true);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                m12a.serviceMethod();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

        final MyThread12 m12b = new MyThread12(false);
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                m12b.serviceMethod();
            }
        };
        Thread thread1 = new Thread(runnable1);
        thread1.start();
    }
}
